<?php

namespace App\Http\Logic;

use App\Models\FamilyFund;
use App\Models\FamilyMember;
use App\Models\FundTransaction;
use Carbon\Carbon;

class FundLogic
{
    /**
     * 创建家庭基金
     * @param array $params 请求参数
     * @param int $userId 创建者ID
     * @return array 创建结果
     */
    public function createFund(array $params, int $userId): array
    {
        // 创建基金会记录
        $fund = new FamilyFund();
        $fund->fund_name = $params['fund_name'];
        $fund->description = $params['description'] ?? '集小善为大爱，一个家温暖一个社会。';
        $fund->creator_id = $userId;
        $fund->current_balance = 0;
        $fund->save();

        // 创建基金会成员记录，将创建者设为管理员
        $member = new FamilyMember();
        $member->fund_id = $fund->id;
        $member->user_id = $userId;
        $member->is_admin = 1; // 管理员角色
        $member->save();

        return [
            'fund_id' => $fund->id,
            'fund_name' => $fund->fund_name,
        ];
    }

    /**
     * 获取基金详情
     * @param int $fundId 基金ID
     * @return array 基金详情
     */
    public function getFundDetails(int $fundId): array
    {
        // 查询基金基本信息
        $fund = FamilyFund::where('id', $fundId)
            ->first();

        if (!$fund) {
            return ['error' => '基金不存在']; // 实际应用中应抛出异常
        }

        // 查询基金成员信息
        $members = FamilyMember::where('fund_id', $fundId)->get();

        // 格式化成员数据
        $memberList = [];
        foreach ($members as $member) {
            $memberList[] = [
                'id' => $member->id,
                'user_id' => $member->user_id,
                'is_admin' => $member->is_admin,
                'created_at' => $member->created_at
            ];
        }

        // 返回基金详情
        return [
            'fund_id' => $fund->id,
            'fund_name' => $fund->fund_name,
            'description' => $fund->description,
            'creator_id' => $fund->creator_id,
            'current_balance' => $fund->current_balance,
            'created_at' => $fund->created_at,
            'cover_image' => $fund->cover_image,
            'carousel_image' => $fund->carousel_image,
            'status' => $fund->status,
            'members' => $memberList
        ];
    }

    //修改基金简介
    public function updateFundDescription(int $fundId, string $description)
    {
        // 查询基金基本信息
        $fund = FamilyFund::find($fundId);
        if (!$fund) {
            return ['error' => '基金不存在']; // 实际应用中应抛出异常
        }
        $arr = FamilyFund::where('id', $fundId)->update(['description' => $description]);
        return $arr;
    }

    /**
     * 开通基金并首次存款
     * @param array $params 请求参数
     * @param int $userId 创建者ID
     * @return array 开通结果
     */
    public function openFund(array $params, int $userId): array
    {
        // 验证最低存款金额
        $amount = $params['amount'] ?? 0;
        if ($amount < 10000) {
            return ['error' => '首次开通基金最低存款10000元'];
        }

        // 创建基金会记录
        $fund = new FamilyFund();
        $fund->fund_name = $params['fund_name'];
        $fund->creator_id = $userId;
        $fund->current_balance = $amount;
        $fund->status = 1; // 激活状态
        $fund->save();

        // 创建基金会成员记录，将创建者设为管理员
        $member = new FamilyMember();
        $member->fund_id = $fund->id;
        $member->user_id = $userId;
        $member->is_admin = 1; // 管理员角色
        $member->save();

        // 记录初始存款交易
        $transaction = new FundTransaction();
        $transaction->fund_id = $fund->id;
        $transaction->user_id = $userId;
        $transaction->amount = $amount;
        $transaction->type = 1; // 1表示存款
        $transaction->status = 1; // 交易成功
        $transaction->description = '基金开通初始存款';
        $transaction->created_at = Carbon::now();
        $transaction->save();

        return [
            'fund_id' => $fund->id,
            'fund_name' => $fund->fund_name,
            'current_balance' => $fund->current_balance,
            'message' => '基金开通成功，初始存款' . $amount . '元'
        ];
    }
}
